package com.luoha.lottery;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.luoha.excel.ExcelHandler;
import com.luoha.random.RandomSort;

public class LotteryActive {
	
	public static synchronized Employee extractLottery(String empId,String empName,List<Employee> employeeList,List<Integer> lotteryList,List<Employee> extractList) throws Exception{
		if(empId == null || empId.equals("")){
			throw new Exception("员工编号不能为空哦~~");
		}
		if(empName == null || empName.equals("")){
			throw new Exception("员工姓名不能为空哦~~");
		}
		boolean flag = false;
		for(Employee employee : employeeList){
			if(employee.getEmpId().equals(empId) && employee.getEmpName().equals(empName)){
				flag = true;
				break;
			}
		}
		if(!flag){
			throw new Exception("员工编号或员工姓名输入有误！");
		}
		if(lotteryList.size() == extractList.size()){
			throw new Exception("奖品已抽完……");
		}
		for(Employee emp : extractList){
			if(empId.equals(emp.getEmpId())){
				throw new Exception("您已抽过奖，请耐心等待奖品~~");
			}
		}
		List<Integer> remainLottery = new ArrayList<Integer>();
		for(Integer i : lotteryList){
			if(!containsLottery(i,extractList)){
				remainLottery.add(i);
			}
			
		}
		int remainCount = remainLottery.size();
		System.out.println(empId+"总个数："+lotteryList.size());
		System.out.println(empId+"extractList的个数："+extractList.size());
		System.out.println(empId+"剩余个数："+remainCount);
		System.out.println(empId+"剩余个数："+(lotteryList.size()-extractList.size()));
		int[] remainNum = new int[remainCount];
		for(int k=0;k<remainCount;k++){
			remainNum[k] = remainLottery.get(k);
		}
		remainLottery.clear();
		RandomSort.randomizeInPlace(remainNum);
		Random random = new Random();
		int index = random.nextInt(remainCount);
		int lotteryId = remainNum[index];
		Employee employee = new Employee();
		employee.setEmpId(empId);
		employee.setEmpName(empName);
		employee.setLotteryId(lotteryId);
		employee.setLotteryName(findLotteryName(lotteryId));
		extractList.add(employee);
		updateExcel(employee);
		return employee;
	}
	
	private static boolean containsLottery(Integer lotteryId,List<Employee> list){
		if (list== null) return false;
		for(Employee emp:list){
			if(emp.getLotteryId().equals(lotteryId)){
				return true;
			}
		}
		return false;
	}
	private static String findLotteryName(int lotteryId){
		if(lotteryId==1){
			return "大鹏VR";
		}else if(lotteryId<=3){
			return "咖啡机";
		}else if(lotteryId<=6){
			return "人体工学椅";
		}else if(lotteryId<=10){
			return "原汁机";
		}else if(lotteryId<=25){
			return "暴风魔镜4代";
		}else if(lotteryId<=50){
			return "智能酸奶机";
		}else if(lotteryId<=80){
			return "小米手环（光感版）";
		}else if(lotteryId<=110){
			return "小米充电宝";
		}else if(lotteryId<=201){
			return "台湾精品肉粽";
		}
		return "";
	}
	
	public static void updateExcel(Employee employee){
		String excelPath = "";
		try {
			excelPath = LotteryActive.class.getClassLoader().getResource("").toURI().getPath();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		excelPath += "resources/员工名单.xlsx";
		ExcelHandler excelHandler = new ExcelHandler();
		try {
			excelHandler.updateExcel(excelPath, employee);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
